package unionsign

import (
	"fmt"
)

type prepare2Message struct{
	tradeHash [32]byte
	T24Set1 [N/3+1]messageT24
	priSeq int
}

func prepare2(priSeq int,prepare2TradeHash [32]byte,T24Set [N/3+1]messageT24)(prepare2Message){
	var prepare2 prepare2Message
	prepare2.tradeHash=prepare2TradeHash
	prepare2.T24Set1=T24Set
	prepare2.priSeq=priSeq
	return prepare2
}

func priGetMessageFromT24(T24 *[2*N/3+1]messageT24,round int,priseq int)([N/3+1] messageT24,[32]byte){//获取一致的交易信息
	var prepareSet1Veried [2*N/3+1] prepare1Message
	var commitSet1Veried [2*N/3+1] commit1Message
	var prepare1Same [2*N/3+1] int
	var counter [2*N/3+1] int
	var mesSet[N/3+1] messageT24
	var prepare2Tradehash [32]byte
	var commit2Tradehash [32]byte
	var h [32]byte

	for i:=0;i<2*N/3+1;i++{
		prepare1Same[i]=N
	}

	count:=0
	for i:=0;i<2*N/3+1;i++{//获取消息与签名一致的prepare1
		prepareSet1Veried[i]=T24[i].prepare1
		commitSet1Veried[i]=T24[i].commit1
	}

	for i:=0;i<2*N/3+1;i++{
		if prepareSet1Veried[i].tradeHash!=h{
			if prepare1Same[i]==N{
				counter[i]=1
				prepare1Same[i]=i
				for j:=i+1;j<2*N/3;j++{
					if prepareSet1Veried[j].tradeHash==prepareSet1Veried[i].tradeHash&&commitSet1Veried[j].tradeHash==commitSet1Veried[i].tradeHash{
						counter[i]++
						prepare1Same[j]=i

					}
				}
			}
		}
	}
	count=0
	for i:=0;i<2*N/3+1;i++{
		if counter[i]>N/3{
			prepare2Tradehash=prepareSet1Veried[i].tradeHash
			commit2Tradehash=commitSet1Veried[i].tradeHash
			mesSet[count]=T24[i]
			count=1
			for j:=i+1;j<2*N/3+1;j++{
				if count==N/3+1{
					break;
				}
				if prepare1Same[j]==i{
					mesSet[count]=T24[j]
					count++
				}
			}
			//println(countSame[i])
		}
	}
	if round>1&&commit2Tradehash==h{
		fmt.Printf("主节点出错%d\n",priseq)
	}
	return mesSet,prepare2Tradehash
}
